const app = getApp();
import services from '../../services/clockin';
import moment from 'moment';
Page({
  data: {
    isReady: false,
    isLoading: false,
    unionid: '',
    mode: 0,
    modeOptions: [
      {
        title: '工地维度'
      },
      {
        title: '员工维度'
      }
    ],
    formData1: {
      statistic_day: [] // 日期
    },
    formData2: {
      statistic_day: [] // 日期
    },
    stationList: [],
    shiftTypeList: [
      {
        label: '白班',
        value: 0
      },
      {
        label: '夜班',
        value: 1
      }
    ],
    tableData: []
  },
  async onLoad() {
    const { unionid } = app.globalData.userInfo || {};
    this.setData({
      unionid: unionid || '2QV8HeciS9zgYhRAtxbpiPZwiEiE'
    });
    await this.initData();
    this.setData({
      isReady: true
    });
  },
  async onShow() {
    if (!this.data.isReady) {
      await this.initData();
    }
  },
  async initData() {
    this.setData({
      formData1: {
        statistic_day: [moment().subtract(1, 'month'), moment()]
      },
      formData2: {
        statistic_day: [moment().subtract(1, 'month'), moment()]
      }
    });
    this.handleSearch();
  },
  handleSearch() {
    if (this.data.mode === 0) {
      this.queryGetReportStationPeriod();
    } else {
      this.queryGetReportEmployeePeriod();
    }
  },
  handleChangeMode(current) {
    this.setData({
      mode: current || 0
    });
    switch (current) {
      case 0: {
        this.setData({
          tableData: []
        });
        this.queryGetReportStationPeriod();
        break;
      }
      case 1: {
        this.setData({
          tableData: []
        });
        this.queryGetReportEmployeePeriod();
        break;
      }
    }
  },
  handleChangeDay(value) {
    console.log('handleChangeDay', value);
    if (this.data.mode === 0) {
      this.setData({
        formData1: {
          ...this.data.formData1,
          statistic_day: value
        }
      });
    } else {
      this.setData({
        formData2: {
          ...this.data.formData2,
          statistic_day: value
        }
      });
    }
  },
  async queryGetReportEmployeePeriod() {
    const { statistic_day } = this.data.formData2;
    if (!statistic_day || statistic_day.length !== 2) {
      dd.showToast({
        type: 'info',
        content: '请选择日期',
        duration: 1000
      });
      return;
    }
    if (this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true
    });
    try {
      const params = {
        start_day: moment(statistic_day[0]).format('YYYYMMDD'),
        end_day: moment(statistic_day[1]).format('YYYYMMDD')
      };
      const res = await services.getReportEmployeePeriod(params, this.data.unionid);
      console.log('queryGetReportEmployeePeriod', this.formData2, params, res);
      if (res && res.data) {
        this.setData({
          tableData: res.data
        });
        console.log('tableData', this.data.tableData);
      }
    } catch (error) {
      console.log('error', error);
      this.setData({
        tableData: []
      });
    } finally {
      this.setData({
        isLoading: false
      });
    }
  },
  async queryGetReportStationPeriod() {
    const { statistic_day } = this.data.formData1;
    if (!statistic_day || statistic_day.length !== 2) {
      dd.showToast({
        type: 'info',
        content: '请选择日期',
        duration: 1000
      });
      return;
    }
    if (this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true
    });
    try {
      const params = {
        start_day: moment(statistic_day[0]).format('YYYYMMDD'),
        end_day: moment(statistic_day[1]).format('YYYYMMDD')
      };
      const res = await services.getReportStationPeriod(params, this.data.unionid);
      console.log('queryGetReportStationPeriod', this.formData1, params, res);
      if (res && res.data) {
        this.setData({
          tableData: res.data
        });
        console.log('tableData', this.data.tableData);
      }
    } catch (error) {
      console.log('error', error);
      this.setData({
        tableData: []
      });
    } finally {
      this.setData({
        isLoading: false
      });
    }
  }
});
